package com.OrderingSystem.controller.seller;

import com.OrderingSystem.pojo.ProductCategory;
import com.OrderingSystem.service.ProductCategoryService;
import com.OrderingSystem.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = "餐品类别管理接口")
@RestController
@RequestMapping("/seller/productCategory")
@ApiResponses({
        @ApiResponse(code = 200, message = "请求成功",response = Result.class),
        @ApiResponse(code = 403, message = "没有权限"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "服务器错误")
})
public class ProductCategoryController {

    @Autowired
    ProductCategoryService productCategoryService;

    @ApiOperation(value = "查询所有餐品类别(不带分页)", notes = "查询所有餐品类别(不带分页)")
    @GetMapping("/list")
    public Result selectAllProductCategory(){
        List<ProductCategory> productCategories = productCategoryService.selectAllProductCategory();
        return Result.success(productCategories);
    }

    @ApiOperation(value = "查询所有餐品类别(带分页)", notes = "查询所有餐品类别(带分页)")
    @GetMapping("/page")
    public Result selectAllProductCategory(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                           ProductCategory productCategory){
        PageHelper.startPage(pageNum, pageSize);
        List<ProductCategory> productCategories = productCategoryService.selectAllProductCategory(pageNum, pageSize,productCategory);
        PageInfo<ProductCategory> pageInfo = new PageInfo<>(productCategories);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "根据ID查询餐品类别", notes = "根据ID查询餐品类别")
    @GetMapping("/{categoryId}")
    public Result selectProductCategoryById(@PathVariable(value = "categoryId") Integer categoryId){
        ProductCategory productCategory = productCategoryService.getById(categoryId);
        return Result.success(productCategory);
    }

    @ApiOperation("添加餐品类别")
    @PostMapping("/add")
    public Result add(@RequestBody ProductCategory productCategory){
        boolean save = productCategoryService.save(productCategory);
        return Result.success(save);
    }

    @ApiOperation("根据ID更新餐品类别")
    @PutMapping("/update")
    public Result update(@RequestBody ProductCategory productCategory){
        boolean update = productCategoryService.updateById(productCategory);
        return Result.success(update);
    }

    @ApiOperation("根据ID删除餐品类别")
    @DeleteMapping("/delete/{categoryId}")
    @ApiImplicitParam(name = "categoryId", value = "餐品类目ID", required = true, dataType = "Integer")
    public Result delete(@PathVariable(value = "categoryId") Integer categoryId){
        boolean remove = productCategoryService.removeById(categoryId);
        return Result.success(remove);
    }

}
